﻿using Common;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.Services
{
    public class HttpRestClient
    {
        private readonly string apiUrl;
        protected readonly RestClient client;

        public HttpRestClient(string apiUrl)
        {
            this.apiUrl = apiUrl;
            client = new RestClient();
        }

        public async Task<ApiResponse> ExecuteAsync(BaseRequest baseRequest)
        {
            try
            {
                var request = new RestRequest(baseRequest.Method);
                request.AddHeader("Content-Type", baseRequest.ContentType);
                if (!string.IsNullOrEmpty(AppSession.Instance.Token))
                {
                    request.AddHeader("Authorization", "Bearer "+AppSession.Instance.Token);
                }
                if (baseRequest.Parameter != null)
                    request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
                client.BaseUrl = new Uri(apiUrl + baseRequest.Route);
                var response = await client.ExecuteAsync(request);
                switch (response.StatusCode)
                {
                    case System.Net.HttpStatusCode.OK:
                    case System.Net.HttpStatusCode.Unauthorized:
                    case System.Net.HttpStatusCode.Forbidden:
                        return JsonConvert.DeserializeObject<ApiResponse>(response.Content);
                    case System.Net.HttpStatusCode.BadGateway:
                    case System.Net.HttpStatusCode.BadRequest:
                        return new ApiResponse("BadGateway：无法找到服务或服务尚未开启", false, null);
                    default :
                        return new ApiResponse(response.ErrorMessage, false, null);
                }
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        
        public async Task<ApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (!string.IsNullOrEmpty(AppSession.Instance.Token))
            {
                request.AddHeader("Authorization", "Bearer " + AppSession.Instance.Token);
            }
            if (baseRequest.Parameter != null)
                request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            client.BaseUrl = new Uri(apiUrl + baseRequest.Route);
            var response = await client.ExecuteAsync(request);

            switch (response.StatusCode)
            {
                case System.Net.HttpStatusCode.OK:
                case System.Net.HttpStatusCode.Unauthorized:
                case System.Net.HttpStatusCode.Forbidden:
                    return JsonConvert.DeserializeObject<ApiResponse<T>>(response.Content);
                default:
                    return new ApiResponse<T>(response.ErrorMessage, false, default);
            }
        }
    }
}
